var pool = require("./config/db.js")

function Countries(countries) {
  this.id = countries.id;
  this.country = countries.country;
  this.flag= countries.flag;
  this.country_code = countries.country_code;
};

module.exports = Countries;

//增
Countries.prototype.save = function(callback) {
    var countries = {
        country: this.country,
        flag: this.flag,
        country_code: this.country_code
    };
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `countries`(`country`,`flag`,`country_code`) VALUES ("test","tt","ts")', function (err, rows, fields) {
            if (err) throw err;

        });
    });
};

//删
Countries.prototype.delete = function(callback) {
    var countries = {
        id:countries.id,
        country: this.country,
        flag: this.flag,
        country_code: this.country_code
    };
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `countries` WHERE country="test"', function (err, rows, fields) {
            if (err) throw err;

        });
    });
};

//改
Countries.prototype.update = function(callback) {
    var countries = {
        id:countries.id,
        country: this.country,
        flag: this.flag,
        country_code: this.country_code
    };
    pool.getConnection(function(err,connection){
        connection.query('UPDATE `countries` SET country = "test",flag = "tt2",country_code = "ts" WHERE country = "test"', function (err, rows, fields) {
            if (err) throw err;

        });
    });
};

//查一个
Countries.prototype.getTen = function(callback) {
    var countries = {
        id:countries.id,
        country: this.country,
        flag: this.flag,
        country_code: this.country_code
    };
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from countries WHERE country="test"', function (err, rows, fields) {
            if (err) throw err;

        });
    });
};

//查
Countries.getTen = function(callback) {

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        connection.query('SELECT * from countries ORDER BY `id` DESC LIMIT 0,10', function (err, rows, fields) {
            if (err){
                callback("查询出错");
            }
            callback(null,rows);
            connection.release();
        });
    });
};
